/*
 * Copyright (C) 2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package top.xbaistack.utils;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import top.xbaistack.common.Code;
import top.xbaistack.exception.BaseException;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 断言工具类
 * <ul>
 * <li>如果断言的结果达不到预期将会抛出 {@link BaseException} 异常，此异常会被 Spring 的全局异常处理拦截到， 从而统一提示给前端一个错误信息。</li>
 * <li>使用断言工具类的主要目的是减少多余业务代码判断，使业务操作更为明确。</li>
 * </ul>
 *
 * @author xbaistack
 * @since 2025/03/25
 */
public class Assert {

    public static final void isNull(Object target, Code code) {
        if (target != null) {
            throw new BaseException(code);
        }
    }

    public static final void isNull(Object target, String message) {
        if (target != null) {
            throw new BaseException(message);
        }
    }

    public static final void notNull(Object target, Code code) {
        if (target == null) {
            throw new BaseException(code);
        }
    }

    public static final void notNull(Object target, String message) {
        if (target == null) {
            throw new BaseException(message);
        }
    }

    public static final void isBlank(CharSequence target, Code code) {
        if (StringUtils.isNotBlank(target)) {
            throw new BaseException(code);
        }
    }

    public static final void isBlank(CharSequence target, String message) {
        if (StringUtils.isNotBlank(target)) {
            throw new BaseException(message);
        }
    }

    public static final void notBlank(CharSequence target, Code code) {
        if (StringUtils.isBlank(target)) {
            throw new BaseException(code);
        }
    }

    public static final void notBlank(CharSequence target, String message) {
        if (StringUtils.isBlank(target)) {
            throw new BaseException(message);
        }
    }

    public static final void isEmpty(Object[] array, Code code) {
        if (ArrayUtils.isNotEmpty(array)) {
            throw new BaseException(code);
        }
    }

    public static final void isEmpty(Object[] array, String message) {
        if (ArrayUtils.isNotEmpty(array)) {
            throw new BaseException(message);
        }
    }

    public static final void isEmpty(Map<?, ?> map, Code code) {
        if (!CollectionUtils.isEmpty(map)) {
            throw new BaseException(code);
        }
    }

    public static final void isEmpty(Map<?, ?> map, String message) {
        if (!CollectionUtils.isEmpty(map)) {
            throw new BaseException(message);
        }
    }

    public static final void isEmpty(Collection<?> collections, Code code) {
        if (!CollectionUtils.isEmpty(collections)) {
            throw new BaseException(code);
        }
    }

    public static final void isEmpty(Collection<?> collections, String message) {
        if (!CollectionUtils.isEmpty(collections)) {
            throw new BaseException(message);
        }
    }

    public static final void notEmpty(Object[] array, Code code) {
        if (ArrayUtils.isEmpty(array)) {
            throw new BaseException(code);
        }
    }

    public static final void notEmpty(Object[] array, String message) {
        if (ArrayUtils.isEmpty(array)) {
            throw new BaseException(message);
        }
    }

    public static final void notEmpty(Map<?, ?> map, Code code) {
        if (CollectionUtils.isEmpty(map)) {
            throw new BaseException(code);
        }
    }

    public static final void notEmpty(Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new BaseException(message);
        }
    }

    public static final void notEmpty(Collection<?> collections, Code code) {
        if (CollectionUtils.isEmpty(collections)) {
            throw new BaseException(code);
        }
    }

    public static final void notEmpty(Collection<?> collections, String message) {
        if (CollectionUtils.isEmpty(collections)) {
            throw new BaseException(message);
        }
    }

    public static final void equals(Object target, Object another, Code code) {
        if (!Objects.equals(target, another)) {
            throw new BaseException(code);
        }
    }

    public static final void equals(Object target, Object another, String message) {
        if (!Objects.equals(target, another)) {
            throw new BaseException(message);
        }
    }

    public static final void notEquals(Object target, Object another, Code code) {
        if (Objects.equals(target, another)) {
            throw new BaseException(code);
        }
    }

    public static final void notEquals(Object target, Object another, String message) {
        if (Objects.equals(target, another)) {
            throw new BaseException(message);
        }
    }

    public static final void isTrue(Boolean target, Code code) {
        if (target == null || !target) {
            throw new BaseException(code);
        }
    }

    public static final void isTrue(Boolean target, String message) {
        if (target == null || !target) {
            throw new BaseException(message);
        }
    }

    public static final void isFalse(Boolean target, Code code) {
        if (target != null && target) {
            throw new BaseException(code);
        }
    }

    public static final void isFalse(Boolean target, String message) {
        if (target != null && target) {
            throw new BaseException(message);
        }
    }

}
